package com.kidback.bbloc259.util;

import android.content.ContentResolver;
import android.util.Log;


import com.kidback.bbloc259.R;
import com.kidback.bbloc259.app.KidApplication;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间戳转换工具类
 * 注：所有方法入参都是时间戳，时间戳的单位统一使用 毫秒
 * Created by meikai on 2019/07/01.
 */
public class TimeUtils {

    public static final String TIME_FORMAT_YYYY_MM = "yyyy-MM";

    public static final String TIME_FORMAT_YYYY_MM_DD_1 = "yyyy.MM.dd";
    public static final String TIME_FORMAT_YYYY_MM_DD_2 = "yyyy-MM-dd";

    public static final String TIME_FORMAT_MM_DD_1 = "MM-dd";


    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_2 = "yyyy年MM月dd日 HH:mm:ss";

    public static final String TIME_FORMAT_MM_DD_HH_MM = "MM月dd日 HH:mm";

    public static final String TIME_FORMAT_HH_MM = "HH:mm";
    public static final String TIME_FORMAT_HH_MM_SS = "HH:mm:ss";

    /**
     * @param timeStamp 时间戳，单位：毫秒
     * @param format    转换的格式，形如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String convert(long timeStamp, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(new Date(timeStamp));

        } catch (Exception e) {
            Log.d("", e.getMessage());
        }
        return "";
    }

    public static String convertYYYYMMDD(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_1);
    }

    public static String convertYYYYMMDD2(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2);
    }

    /**
     * 跟随系统显示 24小时制 或 12小时制
     */
    public static String convertHHMM(long timeStamp) {
        ContentResolver cv = KidApplication.getInstance().getContentResolver();
        String strTimeFormat = android.provider.Settings.System.getString(cv,
                android.provider.Settings.System.TIME_12_24);
        if (StringUtils.equals(strTimeFormat, "24")) {
            return convertHHMM_24(timeStamp);
        } else {
            return convertHHMM_12(timeStamp) + " " + getNoon(timeStamp);
        }
    }

    /**
     * 获取 24小时制下的  HH:mm
     */
    public static String convertHHMM_24(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_HH_MM);
    }

    /**
     * 获取 12小时制下的  HH:mm
     */
    public static String convertHHMM_12(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);

        int hour12JDK = calendar.get(Calendar.HOUR); //jdk 12小时制的 hour值域=[0-11]
        int hour12ui = hour12JDK;  //但在ui显示上，jdk hour=0应该显示为 12
        if (hour12JDK == 0) {
            hour12ui = 12;
        }

        return hour12ui + ":" + String.format("%02d", calendar.get(Calendar.MINUTE));
    }

    public static String convertMMDDHHMM(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_MM_DD_HH_MM);
    }

    /**
     * 获取指定时间戳的 上午/下午
     *
     * @param timeStamp 时间戳，单位：毫秒
     * @return
     */
    public static String getNoon(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);

        String am_pm;
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        if (hour < 12) {
            am_pm = ResourceUtils.getString(R.string.am);
        } else {
            am_pm = ResourceUtils.getString(R.string.pm);
        }

        return am_pm;
    }


    public static LocalDate toLocalDate(long time) {
        return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 判断时间是否为今日(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isToday(long time) {
        LocalDate today = LocalDate.now();
        LocalDate targetDay = toLocalDate(time);
        return today.getYear() == targetDay.getYear()
                && today.getDayOfYear() == targetDay.getDayOfYear();
    }

    /**
     * 判断时间是否为昨天(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isYesterday(long time) {
        LocalDate today = LocalDate.now();
        LocalDate targetDay = toLocalDate(time);
        return today.getYear() == targetDay.getYear()
                && today.getDayOfYear() == targetDay.getDayOfYear() + 1;
    }

    /**
     * 判断时间是否为明天(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isTomorrow(long time) {
        LocalDate today = LocalDate.now();
        LocalDate targetDay = toLocalDate(time);
        return today.getYear() == targetDay.getYear()
                && today.getDayOfYear() == targetDay.getDayOfYear() - 1;
    }

    /**
     * 判断时间是否为本周(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return currentYear == paramYear && paramWeek == currentWeek;
    }

    /**
     * 判断时间是否为下周(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isNextWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return currentYear == paramYear && paramWeek == currentWeek + 1;
    }

    /**
     * 判断时间是否为今年(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisYear(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        return currentYear == paramYear;
    }

    //返回 “星期x”
    public static String getWeekStr(long time, boolean simple) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        int javaDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 1-7 = 日-六
        int day = javaDayOfWeek - 1; // 0-6 = 日-六
        if (day == 0) {
            day = 7; // 1-7 = 一-日
        }
        int index = day - 1;
        index = index % 7;

        if (simple) {
            return ResourceUtils.getStringArray(R.array.array_week_short)[index];
        } else {
            return ResourceUtils.getStringArray(R.array.array_week_long)[index];
        }
    }

}
